package com.mymvpproject;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.mymvpproject.base.User;
import com.mymvpproject.presenter.LoginPresenter;
import com.mymvpproject.view.LoginView;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

import recycle.LoadMoreAdapterWrapper;
import recycle.MyAdapter;
import recycle.MyBaseAdapter;

public class MainActivity extends AppCompatActivity implements LoginView {
    private EditText et_username, et_password;
    private Button btn_login, btn_clear;
    private LoginPresenter mLoginPresenter;
    private ProgressDialog mDialog;


    RecyclerView mRecyclerView;
    MyBaseAdapter mAdapter;
    int loadCount;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        Glide.with(this).load("www.baidu.com").into(new ImageView(this));
    }

    private void initView() {
        mRecyclerView = (RecyclerView) findViewById(R.id.recycler);
//创建被装饰者类实例
        final MyAdapter adapter = new MyAdapter(this);
//创建装饰者实例，并传入被装饰者和回调接口

        mAdapter = new LoadMoreAdapterWrapper(adapter, new LoadMoreAdapterWrapper.OnLoad() {
            @Override
            public void load(int pagePosition, int pageSize, final LoadMoreAdapterWrapper.ILoadCallback callback) {
//此处模拟做网络操作，2s延迟，将拉取的数据更新到adpter中
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        List<String> dataSet = new ArrayList();
                        for (int i = 0; i < 10; i++) {
                            dataSet.add("我是" + i + "一条数据");
                        }
//数据的处理最终还是交给被装饰的adapter来处理
                        adapter.appendData(dataSet);
                        callback.onSuccess();
//模拟加载到没有更多数据的情况，触发onFailure
                        if (loadCount++ == 3) {
                            callback.onFailure();
                        }
                    }
                }, 2000);
            }
        });
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));


        ExecutorService fixedThreadPool = Executors.newFixedThreadPool(5);
        ExecutorService singleThreadPool = Executors.newSingleThreadExecutor();
        ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
        ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(5);
        ScheduledExecutorService singleThreadScheduledPool = Executors.newSingleThreadScheduledExecutor();


        mLoginPresenter = new LoginPresenter(this);
        et_username = (EditText) findViewById(R.id.et_username);
        et_password = (EditText) findViewById(R.id.et_password);
        btn_login = (Button) findViewById(R.id.btn_login);
        btn_clear = (Button) findViewById(R.id.btn_clear);
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mLoginPresenter.login();
//                ToastUtils.showToast(MainActivity.this,"欢迎登陆");
            }
        });

        btn_clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mLoginPresenter.clear();
            }
        });
    }

    void sayHello(@NonNull String msg) {
        Toast.makeText(this, "" + msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public String getHttpAddress() {
        return null;
    }

    @Override
    public String getUsername() {
        return et_username.getText().toString();
    }

    @Override
    public String getPassword() {
        return et_password.getText().toString();
    }

    @Override
    public void showLoading() {
        mDialog = new ProgressDialog(this);
        mDialog.setMessage("登录中");
        mDialog.show();
    }

    @Override
    public void hideLoading() {
        if (mDialog != null) {
            mDialog.cancel();
        }
    }

    @Override
    public void afterSuccess(User user) {

    }

    @Override
    public void showSuccessMsg(String s) {
        Toast.makeText(this, "" + s, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showFailedMsg(String s) {
        Toast.makeText(this, "" + s, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void clearEditText() {
        et_username.setHint("请输入用户名");
        et_password.setHint("请输入密码");
    }


}
